{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import pymysql\n",
    "import sqlalchemy\n",
    "from datetime import datetime\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "localhost = \"192.168.90.193\"\n",
    "localuser = \"gmgbj\"\n",
    "localpwd = 'G7gIVYB9Xk^7'\n",
    "localurl='mysql+pymysql://gmgbj:G7gIVYB9Xk^7@192.168.90.193:3306/Strategy?charset=utf8'\n",
    " \n",
    "    \n",
    "xuyi_aliyun = \"121.40.53.82\"\n",
    "xy_user = \"root\"\n",
    "xy_pwd = \"Fangzhouqwer1069.\"\n",
    "totalurl='mysql+pymysql://root:Fangzhouqwer1069.@121.40.53.82:3306/Strategy?charset=utf8'\n",
    "aliyun= 'mysql+pymysql://dbuser:dbuser@121@rm-bp1witqzl76lpag957o.mysql.rds.aliyuncs.com:3306/strategy?charset=utf8'"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "df = pd.DataFrame(columns=['orderno', 'signalid','market', 'symbol', 'broker',\n",
    "                           'bstype','qty',\n",
    "                           'price', 'totalfee', 'tradedate', 'addtime' ])\n",
    "msg = df.to_sql(\n",
    "            name=\"orders\",\n",
    "            con=localurl,\n",
    "            if_exists='replace',  #'fail'，'replace'，'append'}，默认'fail' \n",
    "          \n",
    "dtype={'orderno':sqlalchemy.types.NVARCHAR(length=18),\n",
    "       'signalid':sqlalchemy.types.INTEGER(),\n",
    "       'broker':sqlalchemy.types.VARCHAR(length=5),\n",
    "       'market':sqlalchemy.types.NVARCHAR(length=2),\n",
    "       'symbol':sqlalchemy.types.NVARCHAR(length=10),\n",
    "       'bstype':sqlalchemy.types.NVARCHAR(length=1),\n",
    "       'price':sqlalchemy.types.DECIMAL(10,4),\n",
    "       'qty': sqlalchemy.types.INTEGER(),\n",
    "       'tradedate':sqlalchemy.types.DATE(),\n",
    "     'addtime': sqlalchemy.types.DATETIME()},\n",
    "  index_label='orderno',\n",
    "            index=False)\n",
    "print(msg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(columns=['orderno', 'signalid','market', 'symbol', 'broker',\n",
    "                           'bstype','qty',\n",
    "                           'price', 'totalfee', 'tradedate', 'addtime' ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['orderno'] = '20210913HK0001'\n",
    "df['signalid'] = '1'\n",
    "df['market'] = 'HK'\n",
    "df['symbol'] = '59463'\n",
    "df['broker'] = 'XY'\n",
    "df['bstype'] = 'B'\n",
    "df['qty'] = '50000'\n",
    "df['price'] = '0.076'\n",
    "df['totalfee'] = ''\n",
    "df['tradedate'] = datetime.now()\n",
    "df['addtime'] = datetime.now()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'orderno': '20210913HK0001',\n",
       " 'signalid': '1',\n",
       " 'market': 'HK',\n",
       " 'symbol': '59463',\n",
       " 'broker': 'XY',\n",
       " 'bstype': 'B',\n",
       " 'qty': '50000',\n",
       " 'price': '0.076',\n",
       " 'totalfee': '',\n",
       " 'tradedate': datetime.datetime(2021, 9, 13, 17, 49, 38, 235242),\n",
       " 'addtime': datetime.datetime(2021, 9, 13, 17, 49, 38, 235242)}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = {'orderno':'20210913HK0001',\n",
    "'signalid': '1',\n",
    "'market': 'HK',\n",
    "'symbol': '59463',\n",
    "'broker': 'XY',\n",
    "'bstype': 'B',\n",
    "'qty': '50000',\n",
    "'price': '0.076',\n",
    "'totalfee': '',\n",
    "'tradedate': datetime.now(),\n",
    "'addtime': datetime.now()}\n",
    "data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'dict' object has no attribute 'head'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-17-f85fc24d69ae>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mdf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mdf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhead\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'dict' object has no attribute 'head'"
     ]
    }
   ],
   "source": [
    "df.loc[]\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "columns = ['orderno', 'signalid','market', 'symbol', 'broker',\n",
    "                           'bstype','qty',\n",
    "                           'price', 'totalfee', 'tradedate', 'addtime']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = [\n",
    "    ['20210913HK0001', '1', 'HK', '59463', 'XY','B','10000', '0.038',\n",
    " '', datetime.now(),datetime.now()],\n",
    "        ['20210913HK0002', '1', 'HK', '17503', 'XY','B','50000', '0.076',\n",
    " '', datetime.now(),datetime.now()],\n",
    "        ['20210913HK0003', '1', 'HK', '66119', 'XY','B','10000', '0.041',\n",
    " '', datetime.now(),datetime.now()]\n",
    "       ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>signalid</th>\n",
       "      <th>market</th>\n",
       "      <th>symbol</th>\n",
       "      <th>broker</th>\n",
       "      <th>bstype</th>\n",
       "      <th>qty</th>\n",
       "      <th>price</th>\n",
       "      <th>totalfee</th>\n",
       "      <th>tradedate</th>\n",
       "      <th>addtime</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>orderno</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20210913HK0001</th>\n",
       "      <td>1</td>\n",
       "      <td>HK</td>\n",
       "      <td>59463</td>\n",
       "      <td>XY</td>\n",
       "      <td>B</td>\n",
       "      <td>10000</td>\n",
       "      <td>0.038</td>\n",
       "      <td></td>\n",
       "      <td>2021-09-13 18:09:50.679215</td>\n",
       "      <td>2021-09-13 18:09:50.679215</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20210913HK0002</th>\n",
       "      <td>1</td>\n",
       "      <td>HK</td>\n",
       "      <td>17503</td>\n",
       "      <td>XY</td>\n",
       "      <td>B</td>\n",
       "      <td>50000</td>\n",
       "      <td>0.076</td>\n",
       "      <td></td>\n",
       "      <td>2021-09-13 18:09:50.679215</td>\n",
       "      <td>2021-09-13 18:09:50.679215</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20210913HK0003</th>\n",
       "      <td>1</td>\n",
       "      <td>HK</td>\n",
       "      <td>66119</td>\n",
       "      <td>XY</td>\n",
       "      <td>B</td>\n",
       "      <td>50000</td>\n",
       "      <td>0.041</td>\n",
       "      <td></td>\n",
       "      <td>2021-09-13 18:09:50.679215</td>\n",
       "      <td>2021-09-13 18:09:50.679215</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               signalid market symbol broker bstype    qty  price totalfee  \\\n",
       "orderno                                                                      \n",
       "20210913HK0001        1     HK  59463     XY      B  10000  0.038            \n",
       "20210913HK0002        1     HK  17503     XY      B  50000  0.076            \n",
       "20210913HK0003        1     HK  66119     XY      B  50000  0.041            \n",
       "\n",
       "                                tradedate                    addtime  \n",
       "orderno                                                               \n",
       "20210913HK0001 2021-09-13 18:09:50.679215 2021-09-13 18:09:50.679215  \n",
       "20210913HK0002 2021-09-13 18:09:50.679215 2021-09-13 18:09:50.679215  \n",
       "20210913HK0003 2021-09-13 18:09:50.679215 2021-09-13 18:09:50.679215  "
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(data, columns = columns)\n",
    "df.set_index('orderno', inplace=True)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>signalid</th>\n",
       "      <th>market</th>\n",
       "      <th>symbol</th>\n",
       "      <th>broker</th>\n",
       "      <th>bstype</th>\n",
       "      <th>qty</th>\n",
       "      <th>price</th>\n",
       "      <th>totalfee</th>\n",
       "      <th>tradedate</th>\n",
       "      <th>addtime</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>orderno</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [signalid, market, symbol, broker, bstype, qty, price, totalfee, tradedate, addtime]\n",
       "Index: []"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.drop(index = df.index)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>signalid</th>\n",
       "      <th>market</th>\n",
       "      <th>symbol</th>\n",
       "      <th>broker</th>\n",
       "      <th>bstype</th>\n",
       "      <th>qty</th>\n",
       "      <th>price</th>\n",
       "      <th>totalfee</th>\n",
       "      <th>tradedate</th>\n",
       "      <th>addtime</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>orderno</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20210913HK0001</th>\n",
       "      <td>1</td>\n",
       "      <td>HK</td>\n",
       "      <td>59463</td>\n",
       "      <td>XY</td>\n",
       "      <td>B</td>\n",
       "      <td>10000</td>\n",
       "      <td>0.038</td>\n",
       "      <td></td>\n",
       "      <td>2021-09-13 18:09:50.679215</td>\n",
       "      <td>2021-09-13 18:09:50.679215</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20210913HK0002</th>\n",
       "      <td>1</td>\n",
       "      <td>HK</td>\n",
       "      <td>17503</td>\n",
       "      <td>XY</td>\n",
       "      <td>B</td>\n",
       "      <td>50000</td>\n",
       "      <td>0.076</td>\n",
       "      <td></td>\n",
       "      <td>2021-09-13 18:09:50.679215</td>\n",
       "      <td>2021-09-13 18:09:50.679215</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20210913HK0003</th>\n",
       "      <td>1</td>\n",
       "      <td>HK</td>\n",
       "      <td>66119</td>\n",
       "      <td>XY</td>\n",
       "      <td>B</td>\n",
       "      <td>50000</td>\n",
       "      <td>0.041</td>\n",
       "      <td></td>\n",
       "      <td>2021-09-13 18:09:50.679215</td>\n",
       "      <td>2021-09-13 18:09:50.679215</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               signalid market symbol broker bstype    qty  price totalfee  \\\n",
       "orderno                                                                      \n",
       "20210913HK0001        1     HK  59463     XY      B  10000  0.038            \n",
       "20210913HK0002        1     HK  17503     XY      B  50000  0.076            \n",
       "20210913HK0003        1     HK  66119     XY      B  50000  0.041            \n",
       "\n",
       "                                tradedate                    addtime  \n",
       "orderno                                                               \n",
       "20210913HK0001 2021-09-13 18:09:50.679215 2021-09-13 18:09:50.679215  \n",
       "20210913HK0002 2021-09-13 18:09:50.679215 2021-09-13 18:09:50.679215  \n",
       "20210913HK0003 2021-09-13 18:09:50.679215 2021-09-13 18:09:50.679215  "
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "df.to_sql(\n",
    "            name=\"orders\",\n",
    "            con=localurl,\n",
    "            if_exists='append')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
